package com.itheima.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.itheima.pojo.Link;
import com.itheima.service.LinkService;
import com.itheima.utils.ResultUtil;
import com.itheima.vo.LinkVO;
import com.itheima.vo.TodoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/link")
@Api(tags = "友情链接相关接口")
@CrossOrigin
public class LinkController {
    @Autowired
    private LinkService linkService;

    /**
     * 添加友情链接
     *
     * @param linkVO
     * @param request
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("添加友情链接")
    public String add(@RequestBody(required = false) LinkVO linkVO, HttpServletRequest request) {
        try {
            linkService.add(linkVO, request);
            return ResultUtil.result("success", "添加友情链接成功");
        }catch (Exception e){
            e.printStackTrace();
            return ResultUtil.result("401", "添加友情链接失败");
        }
    }

    /**
     * 删除友情链接
     *
     * @param linkVO
     * @param request
     * @return
     */
    @PostMapping("/delete")
    @ApiOperation("删除友情链接")
    public String delete(@RequestBody(required = false) LinkVO linkVO,HttpServletRequest request) {
        if (linkVO.getUid()==null||"".equals(linkVO.getUid())){
            return ResultUtil.result("401", "删除友情链接失败");
        }
        try {
            linkService.delete(linkVO,request);
            return ResultUtil.result("success", "删除友情链接成功");
        }catch (Exception e){
            e.printStackTrace();
            return ResultUtil.result("401", "删除友情链接失败");
        }
    }

    /**
     * 编辑友情链接
     *
     * @param linkVO
     * @param request
     * @return
     */
    @PostMapping("/edit")
    @ApiOperation("编辑友情链接")
    public String edit(@RequestBody(required = false) LinkVO linkVO,HttpServletRequest request) {
        if (linkVO.getUid()==null||"".equals(linkVO.getUid())){
            return ResultUtil.result("401", "编辑友情链接失败");
        }
        try {
            linkService.edit(linkVO,request);
            return ResultUtil.result("success", "删除友情链接成功");
        }catch (Exception e){
            e.printStackTrace();
            return ResultUtil.result("401", "删除友情链接失败");
        }
    }

    /**
     * 查询友情链接
     *
     * @param linkVO
     * @return
     */
    @PostMapping("/getList")
    @ApiOperation("查询友情链接")
    public String getList(@RequestBody(required = false) LinkVO linkVO) {
        try {
            Page<Link> list = linkService.getList(linkVO);
            Map<String, Object> map = new HashMap();
            map.put("records",list.getResult());
            map.put("total",list.getTotal());
            map.put("size",list.getPageSize());
            map.put("current",list.getPageNum());
            map.put("orders","");
            map.put("optimizeCountSql",true);
            map.put("isSearchCount",true);
            return ResultUtil.result("success", map);
        }catch (Exception e){
            e.printStackTrace();
            return ResultUtil.result("401", "查询友情链接失败");
        }
    }

    /**
     * 置顶友情链接
     *
     * @param linkVO
     * @param request
     * @return
     */
    @PostMapping("/stick")
    @ApiOperation("置顶友情链接")
    public String stick(@RequestBody(required = false) LinkVO linkVO,HttpServletRequest request) {
        if (linkVO.getUid()==null||"".equals(linkVO.getUid())){
            return ResultUtil.result("401", "置顶友情链接失败");
        }
        try {
            Page<Link> list = linkService.stick(linkVO, request);
            return ResultUtil.result("success", list);
        }catch (Exception e){
            e.printStackTrace();
            return ResultUtil.result("401", "置顶友情链接失败");
        }
    }
}

